/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package handlers;

import database.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Product;
import model.ProductGroup;
import model.Stock;

/**
 *
 * @author Allan
 */
public abstract class StockListHandler
{
    public static ArrayList<Stock> getAllInStock(int kundeID) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        db.connectToDB();
        
        ResultSet rs = db.query("call getAllInStock(" + kundeID + ")");
        
        ArrayList<Stock> toReturn = new ArrayList();
        
        if(rs.isBeforeFirst())
        {
            while(rs.next())
            {
                ProductGroup productGroup = new ProductGroup(rs.getInt("varegruppeID"), rs.getString("varegruppeNavn"));
                Product product = new Product(rs.getInt("vareID"), rs.getString("vareNavn"), rs.getDouble("stykPris"), productGroup);
                toReturn.add(new Stock(rs.getInt("antal"), product));
            }
        }
        
        db.closeDB();
        return toReturn;
    }
    
    public static ArrayList<Stock> getStock(int kundeID, int varegruppeID) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        db.connectToDB();
        
        ResultSet rs = db.query("call getStock(" + kundeID + ", " + varegruppeID + ")");
        
        ArrayList<Stock> toReturn = new ArrayList();
        
        if(rs.isBeforeFirst())
        {
            while(rs.next())
            {
                ProductGroup productGroup = new ProductGroup(rs.getInt("varegruppeID"), rs.getString("varegruppeNavn"));
                Product product = new Product(rs.getInt("vareID"), rs.getString("vareNavn"), rs.getDouble("stykPris"), productGroup);
                toReturn.add(new Stock(rs.getInt("antal"), product));
            }
        }
        
        db.closeDB();
        return toReturn;
    }
    
    public static ArrayList<Stock> fetchStock(int kID, int vgID) throws ClassNotFoundException, SQLException
    {
        if(vgID == 0)
        {
//            System.out.println("Kunde id i if " + kID);
//            System.out.println("Gruppe id i if " + vgID);
            return getAllInStock(kID);
        }
        else
        {
//            System.out.println("Kunde id i else " + kID);
//            System.out.println("Gruppe id i else " + vgID);
            return getStock(kID, vgID);
        }
    }
}
